home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual BASIC 5.0 (Ent. Edition) / Vb5ent Extractor.EXE / VB / SAMPLES / PGUIDE / PROGWOB / PWOTRNGL.CLS < prev    next >
Encoding:
Visual Basic class definition  |  1996-11-26  |  5.1 KB  |  182 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "Triangle"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Option Explicit
  11. ' >> Best viewed in Full Module view. <<
  12. '
  13. ' Storage for debug ID number.
  14. Private mlngDebugID As Long
  15. Implements IDebug
  16.  
  17. ' The Triangle class implements the IShape
  18. '   interface and the Polygon interface.
  19. '   An inner Polygon object is maintained,
  20. '   and many activities are delegated
  21. '   (drawing, storage of points, and so
  22. '   on).  The Triangle class's own
  23. '   interface has only one method.
  24.  
  25. Implements IShape
  26. Implements Polygon
  27.  
  28. ' The inner Polygon object actually holds
  29. '   the data and does most of the work.
  30. '   The Triangle keeps references to both
  31. '   the Polygon interface and the IShape
  32. '   interface of the inner Polygon.
  33. Private mpyg As Polygon
  34. Private mish As IShape
  35.  
  36. ' -------------------------------------
  37. ' This marks the beginning of the
  38. '   implementation of the IShape interface.
  39.  
  40. ' IShape.DrawToPictureBox is called to
  41. ' ------ ----------------   draw a shape,
  42. '   so each class of shape must supply
  43. '   its own implementation.
  44. '
  45. Private Sub IShape_DrawToPictureBox(ByVal pb As PictureBox)
  46.     ' Delegate to the IShape interface of
  47.     '   the inner Polygon.
  48.     Call mish.DrawToPictureBox(pb)
  49. End Sub
  50.  
  51. ' IShape.TimeTest method is used to show
  52. ' ====== --------       the reduced call
  53. '   overhead of a method called on an
  54. '   interface that several classes
  55. '   implement -- as opposed to calling
  56. '   a similar method on the classes'
  57. '   default interfaces.
  58. '
  59. Private Sub IShape_TimeTest()
  60. End Sub
  61.  
  62. ' -------------------------------------
  63. ' This marks the beginning of the
  64. '   implementation of the Polygon
  65. '   interface.
  66.  
  67. ' Polygon.Color delegates to the inner
  68. ' ======= -----     Polygon object.
  69. '
  70. Private Property Get Polygon_Color() As Long
  71.     Polygon_Color = mpyg.Color
  72. End Property
  73. '
  74. Private Property Let Polygon_Color(ByVal RHS As Long)
  75.     mpyg.Color = RHS
  76. End Property
  77.  
  78. ' Polygon.TimeTest - Triangle has three
  79. ' ======= --------  TimeTest methods, one
  80. '   on the IShape interface (used to show
  81. '   polymorphism and early binding), one on
  82. '   its own interface (used to show late
  83. '   binding), and this one.  This one is
  84. '   a side effect of the fact that Rectangle
  85. '   implements the Polygon interface; it's
  86. '   not used for anything.
  87. Private Sub Polygon_TimeTest()
  88. End Sub
  89.  
  90. ' Polygon.GetPoint
  91. ' ======= --------
  92. '
  93. Private Sub Polygon_GetPoint(ByVal intPoint As Integer, X As Single, Y As Single)
  94.     ' Delegate to inner Polygon.
  95.     Call mpyg.GetPoint(intPoint, X, Y)
  96. End Sub
  97.  
  98. ' Polygon.GetPointCount
  99. ' ======= -------------
  100. '
  101. Private Property Get Polygon_GetPointCount() As Integer
  102.     ' There's no point in delegating to
  103.     '   the inner Polygon -- a triangle
  104.     '   always has three points.
  105.     Polygon_GetPointCount = 3
  106. End Property
  107.  
  108. ' Polygon.SetPoints - When implementing the
  109. ' ======= ---------     SetPoints method
  110. '   of the Polygon interface, the Triangle
  111. '   executes its own code to verify that
  112. '   the input array contains only three
  113. '   points (six Singles), and then
  114. '   delegates to the inner Polygon
  115. '   object.
  116. Private Sub Polygon_SetPoints(asngPoints() As Single)
  117.     Dim blnBadArray As Boolean
  118.     On Error Resume Next
  119.     If UBound(asngPoints) <> 5 Then blnBadArray = True
  120.     If Err.Number <> 0 Then blnBadArray = True
  121.     If blnBadArray Then
  122.         Err.Raise vbObjectError + 2082, , _
  123.             "A triangle is specified by a zero-based array with six values, even values (beginning with 0) being the x-values and odd being the y-values."
  124.         Exit Sub
  125.     End If
  126.     ' Delegate to the inner Polygon, which
  127.     '   completes validation of the array
  128.     '   and stores it.
  129.     Call mpyg.SetPoints(asngPoints)
  130. End Sub
  131.  
  132. ' --------------------------------------
  133. ' This is the beginning of the Triangle's
  134. '   own (default) interface.
  135.  
  136. ' TimeTest method takes no arguments,
  137. ' --------      does nothing, and
  138. '   immediately returns.  It's used to
  139. '   illustrate late binding, as opposed
  140. '   to the early binding provided by
  141. '   calling TimeTest on the IShape
  142. '   interface.
  143. Public Sub TimeTest()
  144. End Sub
  145.  
  146. ' --------------------------------------
  147. ' This is the beginning of the Triangle's
  148. '   private procedures (helper functions
  149. '   and event procedures).
  150.  
  151. Private Sub Class_Initialize()
  152.     Dim asngPoints(0 To 5) As Single
  153.     ' Debug code.
  154.     mlngDebugID = DebugInit(Me)
  155.     '
  156.     ' Create the inner Polygon object, and
  157.     '   get a reference to its IShape
  158.     '   interface.
  159.     Set mpyg = New Polygon
  160.     Set mish = mpyg
  161.     ' Initialize the inner Polygon.
  162.     Call mpyg.SetPoints(asngPoints)
  163. End Sub
  164.  
  165. Private Sub Class_Terminate()
  166.     DebugTerm Me
  167. End Sub
  168.  
  169. ' -------- IDebug Implementation --------
  170. '
  171. ' IDebug.DebugID gives you a way to tell
  172. ' ====== -------    objects apart.  It's
  173. '   required by the DebugInit, DebugTerm,
  174. '   and DebugShow debugging procedures
  175. '   declared in modFriend.
  176. '
  177. Private Property Get IDebug_DebugID() As Long
  178.     IDebug_DebugID = mlngDebugID
  179. End Property
  180.  
  181.  
  182.